package com.zh.note.huawei.twohundred.评论转换输出;

import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/8/23 22:16
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String input = sc.nextLine();
        String input = "hello,2,ok,0,bye,0,test,0,one,1,two,1,a,0";
        String[] comments = input.split(",");
        List<List<String>> tree = new ArrayList<>();
        Deque<String> nodes = new LinkedList<>(Arrays.asList(comments));
        int level = 1;
        while (!nodes.isEmpty()) {
            String comment = nodes.removeFirst();

            if (tree.size() < level) {
                tree.add(new ArrayList<>());
            }
            tree.get(level - 1).add(comment);
            int childNum = Integer.parseInt(nodes.removeFirst());
            dfs(nodes, level + 1, childNum, tree);


        }
        //输出结果
        System.out.println(tree.size());
        for (List<String> list : tree) {
            System.out.println(String.join(",", list));
        }


    }


    private static void dfs(Deque<String> nodes, int level, int childNum, List<List<String>> tree) {
        for (int i = 0; i < childNum; i++) {
            String comment = nodes.removeFirst();

            if (tree.size() < level) {
                tree.add(new ArrayList<>());
            }

            tree.get(level - 1).add(comment);
            int count = Integer.parseInt(nodes.removeFirst());
            if (count > 0) {
                dfs(nodes, level + 1, count, tree);
            }
        }
    }

}
